﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Data;
using System.Data.Objects.SqlClient;
using App.Model;
using App.Domain.Contracts;
using App.Domain.Models;

namespace App.Domain.Handlers
{
    public class GetRecentPosition
    {
        private readonly IApplicationRepository _applicationRepository;
        private readonly IJobPositionRepository _jobPositionRepository;
        private readonly IResumeRepository _resumeRepository;

        public GetRecentPosition(IApplicationRepository applicationRepository,
        IJobPositionRepository jobPositionRepository,
        IResumeRepository resumeRepository)
        {
            _applicationRepository = applicationRepository;
            _jobPositionRepository = jobPositionRepository;
            _resumeRepository = resumeRepository;
        }

        public IEnumerable<RecentApplication> Execute(Guid cumpanyid)
        {
            try
            {
                var list = _applicationRepository.GetAll(cumpanyid).Where(i =>
                    //&& DateTime.Compare( i.DateApplied, DateTime.Now) <= 5
                    SqlFunctions.DateDiff("day", i.StartDate, DateTime.Now) >= 0
                    && SqlFunctions.DateDiff("day", i.EndDate, DateTime.Now) <= 0)
                    .Select(i => new RecentApplication()
                           {
                               ApplicationId = i.Id,
                               JobTitle = i.JobPosition.JobTitle,
                               DesiredSalary = i.Resume.DesiredSalary,
                               DateApplied = i.DateApplied,
                               Status = i.Status == 1 ? "已申请" : "已读取",
                               Rating = "3",

                           });


                return list;
            }
            catch (InvalidOperationException ex)
            {
                throw new BusinessServicesException("UnableToRetrieveReminderExceptionMessage", ex);
            }
        }
    }
}
